package com.rice.generator;

import java.io.*;

/**
 * 动态生成jar包
 * @author ricejson
 */
public class JarGenerator {
    public static void doGenerate(String outputPath) throws IOException, InterruptedException {
        // 定义执行的命令
        String winMvnCommand = "mvn.cmd clean package -DskipTests=true";
        String otherMvnCommand = "mvn clean package -DskipTests=true";
        String mvnCommand = winMvnCommand;
        // 创建出Process对象
        ProcessBuilder processBuilder = new ProcessBuilder(mvnCommand.split(" "));
        // 设置jar包目录
        processBuilder.directory(new File(outputPath));
        // 获取process对象并执行命令
        Process process = processBuilder.start();
        // 读取命令输出
        InputStream inputStream = process.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        // 等待命令执行完毕
        int exitCode = process.waitFor();
        System.out.println("maven打包完毕，退出码: " + exitCode);
    }
}
